﻿using HRX.PayCenter.ApplicationCore.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Text;

namespace HRX.PayCenter.Infrastructure.Data
{
    /// <summary>
    /// 上下文
    /// </summary>
    public class CatalogContext : DbContext
    {
        public CatalogContext(DbContextOptions<CatalogContext> options) : base(options)
        {
        }

        public DbSet<AlipayOrderEntity> AlipayOrders { get; set; }
        public DbSet<DiviseEntity> Divises { get; set; }
        public DbSet<RefundEntity> Refunds { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<AlipayOrderEntity>(ConfigureOrderInfo);
            builder.Entity<DiviseEntity>(ConfigureDiviseInfo);
            builder.Entity<RefundEntity>(ConfigureRefundInfo);
        }

        private void ConfigureOrderInfo(EntityTypeBuilder<AlipayOrderEntity> builder)
        {
            builder.ToTable("Order_Info");

            builder.HasKey(ci => ci.Id);
            builder.Property(ci => ci.Id)
                //.ForSqlServerUseSequenceHiLo("Order_Info_hilo")
                .IsRequired();

            builder.Property(ci => ci.OrderNo)
                .IsRequired(true)
                .HasMaxLength(50);
        }

        private void ConfigureDiviseInfo(EntityTypeBuilder<DiviseEntity> builder)
        {
            builder.ToTable("Order_DiviseInfo");

            builder.HasKey(ci => ci.Id);

            builder.Property(ci => ci.Id)
               //.ForSqlServerUseSequenceHiLo("order_diviseinfo_hilo")
               .IsRequired();

            builder.Property(cb => cb.OrderNo)
                .IsRequired()
                .HasMaxLength(50);
        }

        private void ConfigureRefundInfo(EntityTypeBuilder<RefundEntity> builder)
        {
            builder.ToTable("Order_RefundInfo");

            builder.HasKey(ci => ci.Id);

            builder.Property(ci => ci.Id)
               //.ForSqlServerUseSequenceHiLo("order_refundinfo_hilo")
               .IsRequired();

            builder.Property(cb => cb.OrderNo)
                .IsRequired()
                .HasMaxLength(50);

            builder.Property(cb => cb.RefundNo)
             .IsRequired()
             .HasMaxLength(50);
        }
    }
}

